# -*- coding: utf-8 -*-
"""
soft voting classifier
Created on Sat Apr 28 09:05:09 2018

@author: Allen
"""

import numpy as np
import matplotlib.pyplot as plt

from sklearn import datasets

X, y = datasets.make_moons( n_samples = 500, noise = 0.3, random_state = 42 )

plt.scatter( X[y==0,0], X[y==0,1] )
plt.scatter( X[y==1,0], X[y==1,1] )
plt.show()

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split( X, y )

from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier


### 使用Hard Voting Classifier
from sklearn.ensemble import VotingClassifier
voting_clf = VotingClassifier([
            ( "log_clf", LogisticRegression() ),
            ( "svm_clf", SVC() ),
            ( "dt_clf", DecisionTreeClassifier( random_state = 666 ) )
        ], voting = "hard" )
voting_clf.fit( X_train, y_train )
print( voting_clf.score( X_test, y_test ) ) # 0.896

### soft voting classifier
voting_clf2 = VotingClassifier([
            ( "log_clf", LogisticRegression() ),
            ( "svm_clf", SVC( probability = True ) ),
            ( "dt_clf", DecisionTreeClassifier( random_state = 666 ) )
        ], voting = "soft" )
voting_clf2.fit( X_train, y_train )
print( voting_clf2.score( X_test, y_test ) ) # 0.896
'''
一般来说soft要比hard效果要好一些。
'''